/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.rrhh.managed;

import java.util.List;
import javax.faces.bean.ManagedBean;
import sv.com.intesal.clases.abstracts.ViewScopedAbstract;
import sv.com.intesal.clases.others.FormularioName.RRHH.Mtto;
import sv.com.intesal.rrhh.bean.Area;
import sv.com.intesal.rrhh.controlador.AreaControlador;

/**
 *
 * @author ejerez
 */
@ManagedBean (name="areaManaged")
public class AreaManaged extends ViewScopedAbstract {

    private Area selectedArea;
    private List<Area> areas;

    public AreaManaged() {
        this.selectedArea = new Area();
        this.areas = null;
    }

    public void fillGrid() {
        try {
            if (this.areas == null) {
                if (sm.validar_rol_usuario(Mtto.wfrmArea, sm.buscar) == true) {
                    AreaControlador ac = new AreaControlador();
                    this.areas = ac.getAreas();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    public void completeTransaction() {
        try {
            if (this.validar() == true) {
                if (this.selectedArea.getCodigo_a() == null) {
                    if (sm.validar_rol_usuario(Mtto.wfrmArea, sm.agregar) == true) {
                        AreaControlador mc = new AreaControlador();
                        mc.insert(this.selectedArea);
                        m.messageInfo(m.getIinformation());
                        lm.limpiar();
                    }
                } else {
                    if (sm.validar_rol_usuario(Mtto.wfrmArea, sm.modificar) == true) {
                        AreaControlador mc = new AreaControlador();
                        mc.update(this.selectedArea);
                        m.messageInfo(m.getImodificada());
                        lm.limpiar();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    private Boolean validar() throws Exception {
        try {
            Boolean var_return = true;

            if (this.selectedArea.getNombre_a().equals("")) {
                var_return = false;
                m.messageAdvertencia(m.campoError("NOMBRE"));
            }

            return var_return;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    /*SETTERS AND GETTERS*/
    public List<Area> getAreas() {
        return areas;
    }

    public void setAreas(List<Area> areas) {
        this.areas = areas;
    }

    public Area getSelectedArea() {
        if (selectedArea == null) {
            selectedArea = new Area();
        }
        return selectedArea;
    }

    public void setSelectedArea(Area selectedArea) {
        this.selectedArea = selectedArea;
    }
}
